package cn.wizzer.app.web.modules.controllers.user;

import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.user.modules.models.Banner;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.services.BannerService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.List;

@At("/api/banner")
@IocBean
public class ApiBannerController {

    private static final Log log = Logs.get();

    @Reference
    @Inject
    private BannerService bannerService;
    @Reference
    @Inject
    private SysParamService sysParamService;
    @Inject
    @Reference
    private UserService userService;

    /**
     * 根据类型查询广告信息
     *
     * @param type
     * @return
     */
    @At("/findBanner")
    @Ok("json")
    public Result findBannerByType(@Param("type") String type) {
        try {
            //当查询皇冠banner时，若用户的游戏大厅权限关闭则返回空
            if(type.equals("CROWN")){
                User user = userService.fetch(Mvcs.getReq().getHeader("appid"));
                if(!sysParamService.fetch().getGameSwitch() || user == null || !user.isPlayable()){
                    return Result.success();
                }
            }
            List<Banner> bannerList = bannerService.query(Cnd.where("type", "=", type).and("disabled","=","0"));
            return Result.success().addData(bannerList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }
}
